
use tables/estimates_bs_ci, clear 

/*
weekly bounds
- population
- icli 
- non-icli, h-m
- non-icli, h-i
- clear, h-m
- clear, h-i 
*/

** set up for reshape 
rename lb_pop lb_pop0
rename ub_pop ub_pop0 
rename p025_pop lb_pop1
rename p975_pop ub_pop1

foreach t in non clear icli {
	foreach suff in i m {
		rename lb_`t'_`suff' lb_`t'_`suff'0
		rename ub_`t'_`suff' ub_`t'_`suff'0
		rename p025_`t'_`suff' lb_`t'_`suff'1
		rename p975_`t'_`suff' ub_`t'_`suff'1
	}
}

gen lb_pop2 = lb_pop1
gen ub_pop2 = lb_pop2

foreach t in non clear  {
	gen ub_`t'_m2 = ub_`t'_m1 - bias_ub_`t'
	gen ub_`t'_i2 = ub_`t'_i1 - bias_ub_`t'
	gen lb_`t'_m2 = lb_`t'_m1
	gen lb_`t'_i2 = lb_`t'_i1 - bias_lb_`t'
}

keep week lb_* ub_*
reshape long lb_pop ub_pop lb_non_i ub_non_i lb_non_m ub_non_m ///
	lb_icli_m lb_icli_i ub_icli_m ub_icli_i ///
	lb_clear_i lb_clear_m ub_clear_i ub_clear_m , i(week) j(ci)

	bysort week (ci): assert lb_non_m[2] == lb_non_m[3]
	
	
foreach v of varlist lb_* {
	gen s = string(`v', "%5.4f") + ", "
	drop `v'
	rename s `v'
	replace `v' = "[" + `v' if ~ci
	replace `v' = "(" + `v' if  ci
}

foreach v of varlist ub_* {
	gen s = string(`v', "%4.3f")
	drop `v'
	rename s `v'
	replace `v' = `v' + "] &" if ~ci
	replace `v' = `v' + ") &" if  ci
}
gen weeks = string(week, "%td") 
replace weeks  = subinstr(weeks, "2020", "", .) + " & "
replace weeks =  " & " if ci
list weeks lb_pop ub_pop in 1/2
replace ub_clear_i = subinstr(ub_clear_i, "&", "\\" , .)

# delimit ;
outsheet weeks lb_pop ub_pop 
	lb_non_m ub_non_m lb_non_i ub_non_i
	lb_clear_m ub_clear_m lb_clear_i ub_clear_i if month(week)<=7 & ci<=1
	using tables/bounds_by_group_wtd_Mar_Jul.tex, 
	replace noquote noname delimit(" ")
;

# delimit ;
outsheet weeks lb_pop ub_pop 
	lb_non_m ub_non_m lb_non_i ub_non_i
	lb_clear_m ub_clear_m lb_clear_i ub_clear_i if month(week)<=7 & ci ~= 1
	using tables/bounds_by_group_wtd_Mar_Jul_bc.tex, 
	replace noquote noname delimit(" ")
;

outsheet weeks lb_pop ub_pop 
	lb_non_m ub_non_m lb_non_i ub_non_i
	lb_clear_m ub_clear_m lb_clear_i ub_clear_i if month(week)>=8  & ci<=1
	using tables/bounds_by_group_wtd_Aug_Dec.tex, 
	replace noquote noname delimit(" ")
;
	
outsheet weeks lb_pop ub_pop 
	lb_non_m ub_non_m lb_non_i ub_non_i
	lb_clear_m ub_clear_m lb_clear_i ub_clear_i if month(week)>=8  & ci~= 1
	using tables/bounds_by_group_wtd_Aug_Dec_bc.tex, 
	replace noquote noname delimit(" ")
;
# delimit cr
** comparison to Nir's random sample testing 
gen 		wn = 1 if week == mdy(4,24, 2020) 
replace wn = 2 if week == mdy(6, 5, 2020) 

keep if ~missing(wn)

replace ub_clear_i = subinstr(ub_clear_i, "\\", "&", .)

foreach b in pop non_m non_i clear_m clear_i {
	preserve 
	keep lb_`b' ub_`b' ci wn
	
	reshape wide lb_`b' ub_`b', i(ci) j(wn)
	
	replace ub_`b'2 = subinstr(ub_`b'2, "&", "\\" ,.)
	replace ub_`b'2 = ub_`b'2 + "\\" in 2
	
	if "`b'" == "pop" local  name "Population testing"
	if "`b'" == "non_m" local name "Non-ICLI hospitalizations (H-M)"
	if "`b'" == "non_i" local name "Non-ICLI hospitalizations (H-I)"
	
	if "`b'" == "clear_m" local name "Clear cause hospitalizations (H-M)"
	if "`b'" == "clear_i" local name "Cause hospitalizations (H-I)"
	
	gen c0 = "`name' &" in 1
	replace c0 = " & " in 2
	
	list c0 lb_`b'1 ub_`b'1 lb_`b'2 ub_`b'2 , sep(2) noobs
	outsheet c0 lb_`b'1 ub_`b'1 lb_`b'2 ub_`b'2 ///
		using tables/nir_bounds_`b'.tex, replace noquote nonames delimit(" ")
	restore
}

